//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
 **************************************************************************
 */

/**
 * 导航插件类；
 */
define(function (require, exports, module) {
    var NavigationPanelMainFrmEventHandler = require('./NPMainFrmEventHandler');
    /**
     * Plugin of navigation panel
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.NavigationPanelPlugin = function (readerApp) {
        //子窗口列表；需要实现INavigationChildPanel接口;
        var _childPanelList = [],
        //当前处于显示状态的Panel;
            _curVisibleChildPanel = null,
            _readerApp = readerApp,
            appId = _readerApp.getAppId(),
            _self = this,
            _leftLineId = appId + "_leftLine",
        //按钮面板ID
            _tabPanelId = appId + "_tabPanel",
        //存放按钮的ul列表;
            _buttonListId = appId + "_btnList",
        //点击之后展开面板的ID;
            _showPanelBtnId = appId + "_leftpopimg",
        //右侧面板ID，存放子面板内容;
            _treePanelId = appId + "_fxtree_panel",
        //标题面板；
            _titlePanelId = appId + "_biaotou",
        //标题ID;
            _titleId = appId + "_panelTitle",
        //隐藏面板的按钮ID;
            _hidePanelBtnId = appId + "_lefthid",
            _treeSplitterId = appId + "_splitter_navigation_panel",
            _treeMoveSplitterId = appId + "_splitter_navigation_panel__move",
        //右侧面板是否处于显示状态;
            _isContentVisible = readerApp.getOptions().leftPanelShowOnStart;
        var _defaultPanelWidth = 242;
        //创建面板的div内容；
        var _panelSize =
        {
            treePanelWidth: 0,
            treePanelHeight: 0
        };       
    

        function _createHtmlContent() {
            //左侧的按钮面板；
           var leftLine = $("<div id='" + _leftLineId + "' class='leftline switchPoint'><div class='leftlinebody'><div class='leftlineexpand'></div></div></div>");    
           var tabPanel = $("<div id='" + _tabPanelId + "' class='fwr-navigation-bar-tabmenu fwr-hidden' style='height:100%;position:relative'></div>");
           var buttonList = $("<ul id='" + _buttonListId + "' style='height:100%'></ul>");
           var showPanelBtn = $("<li class='fwr-left-panel fwr-left-panel-border-bottom ' style='height:14px;'></li>");
            buttonList.append(showPanelBtn);
            tabPanel.append(buttonList);
            
            //右侧面板；
            var treePanel = $("<div id='" + _treePanelId + "' class='fwr-left-panel-content'  style='position:absolute;overflow: hidden;  top:0; width: 180px; height: 100%;  color:#456176;border-right:1px solid #ddd'></div>");
            //右侧标题面板;
            var titlePanel= $("<div class='fwr-left-panel fwr-left-panel-border-bottom fwr-title-panel' id='" + _titlePanelId + "' style='color:#666666; font-size:13px'></div>");

            var title = $("<div style='text-align: left;font-size: 13px;'><div style='float:left;' id='" + _titleId + "'></div>");
            hidePanelBtn = $("<div id='" + _hidePanelBtnId + "' style='float:right;' class='fwr-hide-panel-btn'></div></div>");
           
            titlePanel.append(title).append(hidePanelBtn);          
            treePanel.append(titlePanel);            
            tabPanel.append(treePanel);

            //拖动层；
            var treeSplitter = $("<div id='" + _treeSplitterId + "' style='height:100%' class='fwr-hidden fwr-windows-splitter' title='Click and drag to resize.'></div>");
            //隐藏的拖动层；
            var treeMoveSplitter =$("<div id='" + _treeMoveSplitterId + "' style='height:100%;position:absolute;left:-10000px;' class='fwr-windows-splitter fwr-top-layer' title='Click and drag to resize.'></div>");           
            
            var leftPanel =  $("#" + _readerApp.getMainView().getLeftPanelID());
            leftPanel.append(leftLine).append(tabPanel).append(treeSplitter).append(treeMoveSplitter);          
        };

        this._bindEvents = function() {
            
            $("#" + _hidePanelBtnId).on("click", function () {
                $("#" + _leftLineId).addClass("expanded");
                _self.changePanelState(true);
                _self.onSize();

            });            
         
            
            $("#" + _leftLineId).on("click", function() {
                $("#" + _leftLineId).toggleClass("expanded");
               _self.changePanelState(_self.isVisible());      
               _self.onSize();             
            });

            var childCount = _childPanelList.length;
            for (var i = 0; i < childCount; i++) {
                var panel = _childPanelList[i];
                $("#" + panel.getTabBtnID()).on('click', { panel: panel }, function (event) {    
                    if( _curVisibleChildPanel == event.data.panel){     
                    }
                    else {
                        _self.showChildPanel(event.data.panel);
                    }                 
                });
            }   

            $(_readerApp).on(WebPDF.EventList.MAINFRAME_RESIZE,_onMainFrmSize);
        }

        this.getSplitterDivID = function () {
            return _treeSplitterId;
        };
        this.getMoveSplitterDivID = function () {
            return _treeMoveSplitterId;
        };
        this.getTreePanelDivID = function () {
            return _treePanelId;
        };
        this.getTabPanelDivID = function () {
            return _tabPanelId;
        };

        this.getPanelWidth = function () {           
            var leftLine = $("#" + _leftLineId);
            var tabPanel = $("#" + _tabPanelId);
            var treePanel = $("#" + _treePanelId);
            var treeSplitter = $("#" + _treeSplitterId);            
            var tabPanelWidth = tabPanel.is(":hidden") ? 0 : tabPanel.outerWidth();
            var treePanelWidth = treePanel.is(":hidden") ? 0 : treePanel.outerWidth();
            var treeSplitterWidth = treeSplitter.is(":hidden") ? 0 : treeSplitter.innerWidth();     
            return leftLine.outerWidth() + tabPanelWidth + treePanelWidth + treeSplitterWidth;
        };

        /**
         * Add child panel
         * @param {WebPDF.INavigationChildPanel} childPanel child panel to add.
         */
        this.addChildPanel = function (childPanel) {
            if (!childPanel)  return;
            _childPanelList.push(childPanel);
        };
        /**
         * Check whether panel is visible.
         * @returns {Boolean} return true if the panel is visible.
         */
        this.isVisible = function () {
            return _isContentVisible;
        };

        /**
         * Change state of panel
         * @param isDisable set true if you want to disable the navigation panel
         * @private
         */
        this.changePanelState = function(isDisable) {           
           var tabPanel =  $("#" + _tabPanelId);
           var treeSplitter = $("#" + _treeSplitterId);
           var showPanelBtn =  $("#" + _showPanelBtnId);
            if (isDisable) {
                tabPanel.addClass("fwr-hidden");   
                /*
                if (_curVisibleChildPanel != null) {                 
                    $("#" + _curVisibleChildPanel.getTabBtnID() + ">div").removeClass(_curVisibleChildPanel.getTabHotClass())
                        .addClass(_curVisibleChildPanel.getTabNorClass());
                }*/
            }
            else {
                tabPanel.removeClass("fwr-hidden");               
            }
            _isContentVisible = !isDisable;             
        };

        //显示某个子panel;
        this.showChildPanel = function (childPanelToShow) {       
            
            if (_curVisibleChildPanel == childPanelToShow) return;
        
            
            if (_curVisibleChildPanel != null) {
                $("#" + _curVisibleChildPanel.getPanelID()).addClass("fwr-hidden");
                $("#" + _curVisibleChildPanel.getTabBtnID() + ">div").removeClass(_curVisibleChildPanel.getTabHotClass());                
                $("#" + _curVisibleChildPanel.getTabBtnID()).removeClass("fwr-tab-selected");               
            }

       
            _curVisibleChildPanel = childPanelToShow;
            
           if (!childPanelToShow){
                return;
            }                 
            
            var jqPanel = $("#" + childPanelToShow.getPanelID());
            if (jqPanel.hasClass("fwr-hidden")) {
                jqPanel.removeClass("fwr-hidden");
                
                childPanelToShow.onSize(_panelSize.treePanelWidth, _panelSize.treePanelHeight);
                $("#" + _titleId).attr("data-i18n" , childPanelToShow.getTitle()).i18n();
                $("#" + childPanelToShow.getTabBtnID() + ">div").addClass(childPanelToShow.getTabHotClass());
                $("#" + childPanelToShow.getTabBtnID()).addClass("fwr-tab-selected");
            }         
        };

        this.onSize = function () {
            var jqLeftPanel = $("#" + _readerApp.getMainView().getLeftPanelID());
            jqLeftPanel.width(_self.getPanelWidth());
            _readerApp.onResize();
        };

        function _onMainFrmSize () {
            if (!_isContentVisible)
                return;
            var jqTreePanel = $("#" + _treePanelId);
            var jqLeftPanel = $("#" + _readerApp.getMainView().getLeftPanelID());
            var leftPanelHeight = jqLeftPanel.height();
            var btnListWidth = $("#" + _buttonListId).width();
            
            //这一句主要是为了兼容ie8，jqTreePanel高度设为100%获取的高度值确是错的;
            jqTreePanel.css({ left: btnListWidth, height: leftPanelHeight });
            $("#" + _treeSplitterId).css({ left: btnListWidth + jqTreePanel.width(), height: leftPanelHeight });
            $("#" + _treeMoveSplitterId).height(leftPanelHeight);
            var treePanelWidth = jqTreePanel.width();
            var treePanelHeight = jqTreePanel.height() - $("#" + _titlePanelId).height();
            _panelSize.treePanelWidth = treePanelWidth;
            _panelSize.treePanelHeight = treePanelHeight;
            if (_curVisibleChildPanel){
                _curVisibleChildPanel.onSize(treePanelWidth, treePanelHeight);
            }
        }

        //获取Plugin名称;
        this.getPluginName = function () {
            return "NavigationPanel Plugin";
        };
        //插件被注册的时候调用;
        this.onRegister = function () {
        };
        //模块初始化;
        this.init = function () {
           _createHtmlContent();
            var childCount = _childPanelList.length;
            var jqBtnList = $("#" + _buttonListId);
            var jqTreePanel = $("#" + _treePanelId);

            for (var i = 0; i < childCount; i++) {
                var panel = _childPanelList[i];                           
                jqBtnList.append("<li class='fwr-left-panel fwr-left-panel-border-bottom fwr-left-panel_border_right' id='" + panel.getTabBtnID() + "' style='cursor:pointer;' data-i18n='[title]ThumbNail.Title'><div class='" + panel.getTabNorClass() + "'/><span class='fwr-active-panel'></span></li>");           
                jqTreePanel.append("<div  id='" + panel.getPanelID() + "' class='fwr-hidden fwrJScrollPaneContainer fwrJScrollPaneScrollable '></div>");
            }
            _self.changePanelState(!_isContentVisible);
            this._bindEvents();
            _readerApp.registerMainFrmEventHandler(new NavigationPanelMainFrmEventHandler(_self));           
            if (childCount > 0) {
                _self.showChildPanel(_childPanelList[0]);
            }            
            $("#" + _treePanelId).i18n();
            
             _self.onSize();
        };

        //模块卸载;
        this.unload = function () {
        };
    };

    exports.createPlugin = function (readerApp) {
        return new WebPDF.NavigationPanelPlugin(readerApp);
    };
});